#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import shutil

import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from orioluspith.packets.scriptreader import ScriptReader

from unittest import TestCase, TestLoader, TextTestRunner

class TestScriptReader(TestCase):
    def setUp(self):
        self.__base_dir = "./testscriptreader"
        self.__target_files = ["preinstall", "postinstall", "install", "preremove", "remove", "postremove"]
        self.__target_dir = os.path.abspath(self.__base_dir)
        
        if not os.path.exists(self.__target_dir):
            os.mkdir(self.__target_dir)
             
        map(self.__create_file, self.__target_files) 

    def tearDown(self):
        map(self.__delete_file, self.__target_files)        
        if os.path.exists(self.__target_dir):
            shutil.rmtree(self.__target_dir)

    def test_sript_reader(self):
        scripts = ScriptReader().read(self.__base_dir)
        self.assertEqual(len(self.__target_files), len(scripts))
        for script_name in self.__target_files:
            self.assertTrue(script_name, scripts[script_name].name)
               
            path = os.path.abspath(os.path.join(self.__base_dir, script_name) + ".py")
            self.assertEqual(path, scripts[script_name].path)
            self.assertTrue(os.path.exists(path))
        
    def __delete_file(self, file_name):
        file_path = os.path.abspath(os.path.join(self.__base_dir, file_name)) + ".py"
        if os.path.exists(file_path):
            os.remove(file_path)
            
    def __create_file(self, file_name):
        file_path = os.path.abspath(os.path.join(self.__base_dir, file_name)) + ".py"
        if not os.path.exists(file_path):
            fd = file(file_path,'w')
            fd.close()

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestScriptReader)
    TextTestRunner(verbosity=2).run(suite)